package com.luck.emp.report.web;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.luck.emp.entity.EmpTWarehouse;
import com.luck.emp.kind.logic.IKindLogic;
import com.luck.emp.report.logic.IReportLogic;
import com.luck.emp.report.qry.ReportQry;
import com.luck.emp.report.vo.BillVo;
import com.luck.emp.report.vo.OutinVo;
import com.luck.emp.report.vo.StorageVo;
import com.luck.emp.wh.logic.IWhLogic;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

public class ReportAction extends BaseActionSupport {

	private static final long serialVersionUID = -7771675382228954116L;
	IReportLogic reportLogic;
	IWhLogic whLogic;
	ReportQry qry;
	IKindLogic kindLogic;
	List<StorageVo> storageVos;
	List<BillVo> billVos;
	List<EmpTWarehouse> whList;
	Page page;
	List<OutinVo> outins;

	// 库存情况统计
	public String storage() throws Exception {
		UserBean userInfo = super.getUserInfo();

		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (ReportQry) ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new ReportQry();
		}
		if (qry.getDeptId() == null) {
			qry.setDeptId(userInfo.getDeptId());
			qry.setDeptName(userInfo.getDept().getDeptName());
		}
		if (qry.getStartDate() == null)
			qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(
					DateUtil.getDate(), -30)));
		if (qry.getEndDate() == null)
			qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
		qry.setSearchProductType("2");//只查库存>0的
		qry.setUserInfo(userInfo);
		if (StringUtils.isNotBlank(qry.getOperateType())) {
			storageVos = reportLogic.findStorage(qry, List.class);
		}
		return "storage";
	}

	// 单据查询
	public String bill() throws Exception {
		UserBean userInfo = super.getUserInfo();

		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (ReportQry) ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new ReportQry();
		}
		if (qry.getStartDate() == null)
			qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(
					DateUtil.getDate(), -30)));
		if (qry.getEndDate() == null)
			qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
		qry.setUserInfo(userInfo);
		if (StringUtils.isNotBlank(qry.getOperateType()))
			billVos = reportLogic.findBill(qry);
		return "bill";
	}

	// 销售情况统计
	public String sales() throws Exception {
		UserBean userInfo = super.getUserInfo();

		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (ReportQry) ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new ReportQry();
		}
		if (qry.getStartDate() == null)
			qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(
					DateUtil.getDate(), -30)));
		if (qry.getEndDate() == null)
			qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
		qry.setUserInfo(userInfo);
		if(qry.getDeptId() == null){
			qry.setDeptId(userInfo.getDeptId());
			qry.setDeptName(userInfo.getDept().getDeptName());
		}
		if (StringUtils.isNotBlank(qry.getOperateType()))
			page = reportLogic.findSalePage(qry,Page.class);
		return "sales";
	}
	public String outinqk() throws Exception{
		UserBean userInfo = super.getUserInfo();

		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (ReportQry) ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new ReportQry();
		}
		if (qry.getStartDate() == null)
			qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(
					DateUtil.getDate(), -30)));
		if (qry.getEndDate() == null)
			qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
		qry.setUserInfo(userInfo);
		if(qry.getDeptId() == null){
			qry.setDeptId(userInfo.getDeptId());
			qry.setDeptName(userInfo.getDept().getDeptName());
		}
		if (StringUtils.isNotBlank(qry.getOperateType()))
			outins = reportLogic.findOutinqkPage(qry);
		return "outinqk";
	}

	public IReportLogic getReportLogic() {
		return reportLogic;
	}

	public void setReportLogic(IReportLogic reportLogic) {
		this.reportLogic = reportLogic;
	}

	public IWhLogic getWhLogic() {
		return whLogic;
	}

	public void setWhLogic(IWhLogic whLogic) {
		this.whLogic = whLogic;
	}

	public ReportQry getQry() {
		return qry;
	}

	public void setQry(ReportQry qry) {
		this.qry = qry;
	}

	public IKindLogic getKindLogic() {
		return kindLogic;
	}

	public void setKindLogic(IKindLogic kindLogic) {
		this.kindLogic = kindLogic;
	}

	public List<StorageVo> getStorageVos() {
		return storageVos;
	}

	public void setStorageVos(List<StorageVo> storageVos) {
		this.storageVos = storageVos;
	}

	public List<BillVo> getBillVos() {
		return billVos;
	}

	public void setBillVos(List<BillVo> billVos) {
		this.billVos = billVos;
	}

	public List<EmpTWarehouse> getWhList() {
		return whList;
	}

	public void setWhList(List<EmpTWarehouse> whList) {
		this.whList = whList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<OutinVo> getOutins() {
		return outins;
	}

	public void setOutins(List<OutinVo> outins) {
		this.outins = outins;
	}

}
